package run.siyuan.rabbitmq.plugins.retry.message.controller;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @className: IndexController
 * @Description: TODO
 * @author: siyuan
 * @date: 2022/5/17 10:54 AM
 */
@RestController
public class IndexController {

    @Autowired
    RabbitTemplate rabbitTemplate;

    @GetMapping(value = "/plugin/send")
    public String pluginMsgSend(@RequestParam Integer time) {
        JSONObject json = new JSONObject();
        json.set("name", "插件延迟消息");
        json.set("time", System.currentTimeMillis());
        json.set("delayTime", time);

        MessageProperties messageProperties = new MessageProperties();
        messageProperties.setHeader("x-delay", 1000 * time);//延迟5秒被删除
        Message message = new Message(JSONUtil.toJsonStr(json).getBytes(), messageProperties);
        rabbitTemplate.convertAndSend("PLUGIN_DELAY_EXCHANGE", "delay", message);//交换机和路由键必须和配置文件类中保持一致
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("消息发送成功【" + sdf.format(new Date()) + "】" + "延迟时间：" + time);
        return "succ";
    }
}

